package testcases;

import java.net.HttpURLConnection;

import javax.ws.rs.core.MultivaluedMap;

import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.jayway.jsonpath.JsonPath;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;

public class TC01 {
  
  private static final Logger logger = Logger.getLogger(TC01.class);
  
  @BeforeMethod
  public void initializeLogger()
  {
    org.apache.log4j.BasicConfigurator.configure();
    
  }
  
  @Test
  public void verifyStatusOK() {
    
    String lat = "-31.41475980";
    String lon = "-64.19687429999999";
    Boolean sensor = false;
    String url = "http://maps.googleapis.com";
    String endpoint = "/maps/api/geocode/json";
    Client client = Client.create();
    WebResource webResource = client.resource(url + endpoint);
    MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
    queryParams.add("latlng", lat + "," + lon);
    queryParams.add("sensor", sensor.toString());
    ClientResponse clientResponse = webResource.queryParams(queryParams).get(
        ClientResponse.class);
    logger.debug(clientResponse);
    Assert.assertEquals(clientResponse.getStatus(), HttpURLConnection.HTTP_OK);
    String responseObject = clientResponse.getEntity(String.class);
    Assert.assertTrue(JsonPath.read(responseObject, "$.status").equals("OK"));
  }
  
  @Test
  public void verifyFormattedAddress() {
    
    String lat = "-31.41475980";
    String lon = "-64.19687429999999";
    Boolean sensor = false;
    String url = "http://maps.googleapis.com";
    String endpoint = "/maps/api/geocode/json";
    Client client = Client.create();
    WebResource webResource = client.resource(url + endpoint);
    MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
    queryParams.add("latlng", lat + "," + lon);
    queryParams.add("sensor", sensor.toString());
    ClientResponse clientResponse = webResource.queryParams(queryParams).get(
        ClientResponse.class);
    logger.debug(clientResponse);
    Assert.assertEquals(clientResponse.getStatus(), HttpURLConnection.HTTP_OK);
    String responseObject = clientResponse.getEntity(String.class);
    Assert.assertEquals(JsonPath.read(responseObject,
        "$.results[0].formatted_address[*]"), (
        "Caseros 800-898, Córdoba, Córdoba Province, Argentina"));
  }
  
  @Test
  public void verifyDeleteIsNotAllowed()
  {
    String lat = "-31.41475980";
    String lon = "-64.19687429999999";
    Boolean sensor = false;
    String url = "http://maps.googleapis.com";
    String endpoint = "/maps/api/geocode/json";
    Client client = Client.create();
    WebResource webResource = client.resource(url + endpoint);
    MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
    queryParams.add("latlng", lat + "," + lon);
    queryParams.add("sensor", sensor.toString());
    ClientResponse clientResponse = webResource.queryParams(queryParams)
        .delete(
            ClientResponse.class);
    Assert.assertEquals(clientResponse.getStatus(),
        HttpURLConnection.HTTP_BAD_METHOD);
  }
  
  @Test
  public void verifyPutIsNotAllowed()
  {
    String lat = "-31.41475980";
    String lon = "-64.19687429999999";
    Boolean sensor = false;
    String url = "http://maps.googleapis.com";
    String endpoint = "/maps/api/geocode/json";
    Client client = Client.create();
    WebResource webResource = client.resource(url + endpoint);
    MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
    queryParams.add("latlng", lat + "," + lon);
    queryParams.add("sensor", sensor.toString());
    ClientResponse clientResponse = webResource.queryParams(queryParams).put(
        ClientResponse.class);
    Assert.assertEquals(clientResponse.getStatus(),
        HttpURLConnection.HTTP_LENGTH_REQUIRED);
  }
}
